Linq is very easy to read and simplifies the code. In this example we will learn about grouping and sorting using linq. Here we are filtering and Listing the group of words by string length from a string array.
Example:
static void Main(string[] args)
{
string[] words = { "Frequency", "wonderful", "Honeypot", "Halftone", "Hyperlink", "Navigation", "Footer" };
var groups = from word in words
orderby word ascending
group word
by word.Length into lengthGroups
orderby lengthGroups.Key descending
select new { Length = lengthGroups.Key, Words = lengthGroups };
foreach (var group in groups)
{
Console.WriteLine("Words of length " + group.Length);
foreach (string word in group.Words)
Console.WriteLine(" " + word);
}
Console.ReadLine();
}
Output:
Post your comments / questions
Recent Article
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
Related Article